﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindata();
        }
    }
    public void Bindata()
    {
        CustomerBOL cBOL = new CustomerBOL();
        GridView1.DataSource = cBOL.LoadCustomer();
        GridView1.DataBind();
    }
    protected void UpdateRecord(object s, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox tIdentification = (TextBox)row.FindControl("txtIdentification");
        TextBox tFirstname = (TextBox)row.FindControl("txtFirstname");
        TextBox tLastname = (TextBox)row.FindControl("txtLastname");
        TextBox tAge = (TextBox)row.FindControl("txtAge");
        TextBox tAddress = (TextBox)row.FindControl("txtAddress");
        TextBox tPhonenumber = (TextBox)row.FindControl("txtPhonenumber");
        TextBox tEmail = (TextBox)row.FindControl("txtEmail");

        Customer c = new Customer();
        CustomerBOL cBOL = new CustomerBOL();

        try
        {
            c.Identification = tIdentification.Text;
            c.Firstname = tFirstname.Text;
            c.Lastname = tLastname.Text;
            c.Age = Convert.ToInt32(tAge.Text);
            c.Address = tAddress.Text;
            c.Phonenumber = tPhonenumber.Text;
            c.Email = tEmail.Text;

            int result = cBOL.updateCustomer(c);
            if (result > 0)
            {
                LResult.Text = "Update record is successful!";
            }
            else
                LResult.Text = "Update record is error";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {
            c = null;
            cBOL = null;
        }
    }
    protected void EditRecord(object s, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bindata();
    }
    protected void CancelRecord(object s, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void DeleteRecord(object s, GridViewDeleteEventArgs e)
    {
        string tIdentification = GridView1.DataKeys[e.RowIndex].Value.ToString();

        Customer c = new Customer();
        CustomerBOL cBOL = new CustomerBOL();
        try
        {
            c.Identification = tIdentification;
            int result = cBOL.deleteCustomer(c);
            if (result > 0)
                LResult.Text = "Delete record is successful.";
            else
                LResult.Text = "Delete record is error.";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {

            c = null;
            cBOL = null;
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
}